/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server.main;

import com.core.utils.Utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

/**
 *
 * @author daz
 */
public class UseCard {
    
    public String urlLink = "http://123.30.178.70:8080/voucher/useCard.html";
    public String issuer = "VINA";//MOBI ,VINA,VT
    public String cardSerial = "ZWK329677";// 11-15 char
    public String cardCode = "21819412694144";
    public long transRef = 1; // ma so duy nhat de doi soat
    public String partnerCode = "inet";//ma doi tac
    public String passWord = "123456";
    public String secretKey = "inet12";
    private String signature;
    private static final String ISSUER = "issuer";//MOBI ,VINA,VT
    private static final String CARDSERIAL = "cardSerial";// 11-15 char
    private static final String CARDCODE = "cardCode";
    private static final String TRANSREF = "transRef"; // ma so duy nhat de doi soat
    private static final String PARTNERCODE = "partnerCode";//ma doi tac
    private static final String PASSWORD = "password";
    private static final String SECRETKEY = "secretKey";
    private static final String SIGNATURE = "signature";
    String separator = "=%s&";
    private String messsage;
    private String reply;
    
    public UseCard(String issuer, String cardCode, String cardSerial, long transRef) {
        this.issuer = issuer;
        this.cardCode = cardCode;
        this.cardSerial = cardSerial;
        this.transRef = transRef;
        signature = MD5(issuer + cardCode + transRef + partnerCode + passWord + secretKey);
        messsage = urlLink + "?&"
                + String.format(ISSUER + separator + CARDSERIAL + separator + CARDCODE
                + separator + TRANSREF + separator + PARTNERCODE + separator + PASSWORD + separator + SIGNATURE + "=%s",
                issuer, cardSerial, cardCode, transRef, partnerCode, passWord, signature);
    }
    
    public void getReply() {
        //Result == id | 
    }
    
    public void excute() throws Exception {
        // Send data
        reply = "";
        URL url = new URL(urlLink);
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        conn.setConnectTimeout(30000);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(messsage);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = "";
        while ((line = rd.readLine()) != null) {
            reply = line;
        }
        wr.close();
        rd.close();
    }
    
    public static String MD5(String md5) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            byte[] array = md.digest(md5.getBytes());
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException e) {
        }
        return null;
    }
    
    public String[] splitReply() {
        String[] splitReply = Utils.split(reply, '|');
        String[] splitResult = (splitReply != null ? splitReply[0].split("==") : reply.split(("==")));
        String[] result;
        if (splitReply != null) {
            result = new String[3];
            result[0] = splitResult[1];
            result[1] = splitReply[1];
            if (splitReply.length == 3) {
                result[2] = splitReply[2];
            }
        } else {
            result = new String[1];
            result[0] = splitResult[1];
        }
        return result;
    }
}
